package com.telra.app.server.data;

import com.telra.app.server.protobuff.OrderProtos.OrderTicket;

import java.util.Date;
import java.util.Random;

/**
 * Struct to store an indivdual order.
 * Also has some static util methods.
 *
 * @author jamesarlet
 *
 */

public class OrderEvent {

    private String itemName;
    private double price;
    private Category category;
    private String key;

    public static final Random generator = new Random();

    public static enum Category {CLOTHING, ACCESSORIES, FOOTWEAR, OUTERWEAR, MISC}

    public OrderEvent(String itemName, Category category, double price) {
        if (price < 0) {
            throw new IllegalArgumentException("Price must be > 0");
        }
        this.itemName = itemName;
        this.price = price;
        this.category = category;
        key = String.valueOf(new Date().getTime());
    }

    public String getKey() {
        return key;
    }


    public String getItemName() {
        return itemName;
    }

    public double getPrice() {
        return price;
    }

    public Category getCategory() {
        return category;
    }

    public String toString() {
        return itemName + "=" + price;
    }

    ////////////////////////////////////////////

    private static String[] PRODUCTS = {
            "dress shirt","jeans","tee-shirt","slacks","shorts",
            "tie","belt","hat","socks","gloves",
            "shoes","sandels","sneakers","slippers","flipflops",
            "raincoat","ski jacket","fleece coat","overcoat","slicker",
            "briefcase","back-pack","watch","jewelry","sunglasses"
    };

    public static OrderEvent generateOrderEvent() {
        int i = generator.nextInt(25);		//0-24
        Category cat;
        double price = 9.99;

        if (i < 5) {
            cat = Category.CLOTHING;
            price += (100 * generator.nextDouble());

        } else if (i >= 5 && i <= 9) {
            cat = Category.ACCESSORIES;
            price += (25 * generator.nextDouble());
        } else if (i >= 10 && i <=14) {
            cat = Category.FOOTWEAR;
            price += (50 * generator.nextDouble());
        } else if (i >= 15 && i <=20) {
            cat = Category.OUTERWEAR;
            price += (150 * generator.nextDouble());
        } else {
            cat = Category.MISC;
            price += (10 * generator.nextDouble());
        }

        return new OrderEvent(PRODUCTS[i], cat, price);

    }

    public static OrderTicket generateOrder() {

        int i = generator.nextInt(25);		//0-24
        OrderTicket.Category cat;
        double price = 9.99;

        if (i < 5) {
            cat = OrderTicket.Category.CLOTHING;
            price += (100 * generator.nextDouble());

        } else if (i >= 5 && i <= 9) {
            cat = OrderTicket.Category.ACCESSORIES;
            price += (25 * generator.nextDouble());
        } else if (i >= 10 && i <=14) {
            cat = OrderTicket.Category.FOOTWEAR;
            price += (50 * generator.nextDouble());
        } else if (i >= 15 && i <=20) {
            cat = OrderTicket.Category.OUTERWEAR;
            price += (150 * generator.nextDouble());
        } else {
            cat = OrderTicket.Category.MISC;
            price += (10 * generator.nextDouble());
        }

        return OrderTicket.newBuilder()
                .setItemName(PRODUCTS[i])
                .setItemCategory(cat)
                .setPrice(price)
                .build();

    }

    public static Category getCategory(String category) {
        if (category == null) throw new IllegalArgumentException("String cannot be null") ;

        if (category.equalsIgnoreCase("CLOTHING")) return Category.CLOTHING;
        if (category.equalsIgnoreCase("MISC")) return Category.MISC;
        if (category.equalsIgnoreCase("ACCESSORIES")) return Category.ACCESSORIES;
        if (category.equalsIgnoreCase("FOOTWEAR")) return Category.FOOTWEAR;
        if (category.equalsIgnoreCase("OUTERWEAR")) return Category.OUTERWEAR;

        throw new IllegalArgumentException("Unknown category") ;

    }

}
